package top.linuyx.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.linuyx.enums.ResponseEnum;
import top.linuyx.exception.GlobalException;
import top.linuyx.pojo.param.UserBindEmailParam;
import top.linuyx.pojo.param.UserUpdateParam;
import top.linuyx.pojo.param.UserUpdatePasswordParam;
import top.linuyx.pojo.response.ResponseData;
import top.linuyx.service.UserService;

import javax.validation.Valid;

/**
 * @Author Linuyx
 * @Description UserController
 * @Date Created in 2021-04-15 10:49
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PutMapping(name = "修改用户信息")
    @RequiresRoles("user")
    @ResponseBody
    public ResponseData<String> updateUser(@Valid @RequestBody UserUpdateParam updateParam) {
        userService.updateUser(updateParam);
        return ResponseData.success("修改成功");
    }

    @PostMapping(value = "/bind-email", name = "绑定邮箱")
    @RequiresRoles("user")
    @ResponseBody
    public ResponseData<String> bindEmail(@Valid @RequestBody UserBindEmailParam bindEmailParam) {
        userService.bindEmail(bindEmailParam);
        return ResponseData.success("邮箱绑定成功");
    }

    @PostMapping(value = "/update-password", name = "修改密码")
    @RequiresRoles("user")
    @ResponseBody
    public ResponseData<String> updatePassword(@Valid @RequestBody UserUpdatePasswordParam updatePasswordParam){
        userService.updatePassword(updatePasswordParam);
        return ResponseData.success("密码修改成功");
    }

    @PostMapping(value = "/upload-image", name = "上传用户图片")
    @RequiresRoles("user")
    @ResponseBody
    public ResponseData<String> uploadImage(MultipartFile multipartFile) {
        if (multipartFile.isEmpty()) {
            throw new GlobalException(ResponseEnum.PARAM_ERROR.getCode(), "图片不能为空");
        }
        userService.uploadImage(multipartFile);
        return ResponseData.success("图片上传成功");
    }

}
